package com.eagle.controller.testmodel;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(value = { "/testmodel" })
public class TestModelController {
	
	@PostConstruct
	public void test(){
		System.out.println("init controller");
	}
	
	/*
	 * Spring MVC 映射示例
	 * @RequestMapping不但支持标准的URL，还支持Ant风格（即?、*和**的字符，参见3.3.2节的内容）的和带{xxx}占位符的URL。以下URL都是合法的：
	 */
	// 一，/user/*/createUser
    //		 匹配/user/aaa/createUser、/user/bbb/createUser等URL。
	
	// 二，/user/**/createUser
    //		 匹配/user/createUser、/user/aaa/bbb/createUser等URL。

	// 三，/user/createUser??
    //		 匹配/user/createUseraa、/user/createUserbb等URL。
	
	// 四，/user/{userId}
    //		 匹配/user/123、user/abc等URL。
	
	// 五，/user/**/{userId}
    //		 匹配/user/aaa/bbb/123、user/aaa/456等URL。
	
	// 六，company/{companyId}/user/{userId}/detail
    //		 匹配company/123/user/456/detail等的URL。

	@RequestMapping(value = { "/index" } , method=RequestMethod.GET  )
    public String loadWidgetData( ModelMap model) {
		model.put("user", "xugang");
		return "z_model";
	}
	
	@RequestMapping(value = { "/index/{userId}" } )
    public String handle2( @PathVariable("userId") String userId  , ModelMap model) {
		System.out.println( userId );
		model.put("user", "xugang");
		return "z_model";
	}
	
	@RequestMapping(value = { "/index/company/{company}/user/{userId}" } )
    public String handle3( @PathVariable("company") String company  ,  @PathVariable("userId") Integer userId  , ModelMap model) {
		System.out.println( company + ":" + userId );
		model.put("user", "xugang");
		return "z_model";
	}
	
	@RequestMapping(value = { "/index/param" } )
    public String handle4(  @RequestParam(value = "company") String company  ,  
    			@RequestParam( value="userId" , required = false  ) Integer userId ,	 ModelMap model   ) {
		System.out.println( company + ":" + userId );
		model.put("user", "xugang");
		return "z_model";
	}

	//绑定对象
	@RequestMapping(value = { "/index/param/obj" } )
    public String handle5(   TestModel testModel  , TestModel2 testModel2 ) {
		System.out.println( testModel.getTid() + ":" + testModel.getName() );
		System.out.println( testModel2.getTid() + ":" + testModel2.getName() );
		return "z_model";
	}
	
	// 带HTTP 对象\
	@RequestMapping(value="/index/httpobj")
	public  String handle6( String userId , HttpServletRequest request, HttpSession session, ModelMap model  ){
		model.put("user", userId);
		return "z_model";
	}
	
	//JSON示例
	
	
	
	
	
}
